package com.renwei.yunlong.fragment;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.IdRes;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.Editable;
import android.text.InputFilter;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.MotionEvent;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RelativeLayout;
import android.widget.ScrollView;
import android.widget.TextView;

import com.google.gson.Gson;
import com.renwei.yunlong.R;
import com.renwei.yunlong.activity.work.CompanySelectActivity;
import com.renwei.yunlong.activity.work.GuideWebActivity;
import com.renwei.yunlong.activity.work.ObjectSelectActivity;
import com.renwei.yunlong.activity.work.PersonSelectActivity;
import com.renwei.yunlong.activity.work.ProjectSelectActivity;
import com.renwei.yunlong.activity.work.ServiceSelectActivity;
import com.renwei.yunlong.adapter.MuchObjectAdapter;
import com.renwei.yunlong.base.BaseFragment;
import com.renwei.yunlong.bean.AssetBean;
import com.renwei.yunlong.bean.MuchAssetBean;
import com.renwei.yunlong.bean.SendSLABean;
import com.renwei.yunlong.global.AppHelper;
import com.renwei.yunlong.http.Api;
import com.renwei.yunlong.http.HttpTaskListener;
import com.renwei.yunlong.utils.JsonMapUtil;
import com.renwei.yunlong.utils.ServiceRequestManager;
import com.renwei.yunlong.utils.StringUtils;
import com.zyf.fwms.commonlibrary.base.baseadapter.BaseRecyclerViewAdapter;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import butterknife.Bind;


public class SendWorkDetailFragment extends BaseFragment implements View.OnClickListener,
        BaseRecyclerViewAdapter.OnItemClickLitener, RadioGroup.OnCheckedChangeListener, HttpTaskListener, View.OnTouchListener {

    public static final int START_FOR_OWNER_CHOOSE = 111; // 业主选择
    public static final int START_FOR_OWNER_PERSON_CHOOSE = 222; // 发起人
    public static final int START_FOR_SERVICE_CHOOSE = 333; // 处理企业
    public static final int START_FOR_SERVICE_PERSON_CHOOSE = 444; // 处理人
    public static final int START_FOR_SERVICE_PROJECT_CHOOSE = 555; // 所属项目
    public static final int START_FOR_SERVICE_OBJECT_CHOOSE = 666; // 服务对象
    public static final int START_FOR_SERVICE_DIR_CHOOSE = 777; // 所属服务
    //ButterKnife 绑定控件
    //region
    @Bind(R.id.tv_send_company)
    TextView tvSendCompany;
    @Bind(R.id.rl_send_company)
    RelativeLayout rlSendCompany;
    @Bind(R.id.tv_send_person)
    TextView tvSendPerson;
    @Bind(R.id.rl_send_person)
    RelativeLayout rlSendPerson;
    @Bind(R.id.tv_deal_company)
    TextView tvDealCompany;
    @Bind(R.id.rl_deal_company)
    RelativeLayout rlDealCompany;
    @Bind(R.id.tv_deal_person)
    TextView tvDealPerson;
    @Bind(R.id.rl_deal_person)
    RelativeLayout rlDealPerson;
    @Bind(R.id.tv_project)
    TextView tvProject;
    @Bind(R.id.tv_title_project)
    TextView tvTitleProject;
    @Bind(R.id.rl_project)
    RelativeLayout rlProject;
    @Bind(R.id.recyclerView)
    RecyclerView recyclerView;
    @Bind(R.id.rl_object_add)
    RelativeLayout rlObjectAdd;
    @Bind(R.id.tv_urgent)
    TextView tvUrgent;
    @Bind(R.id.rb_t1)
    RadioButton rbT1;
    @Bind(R.id.rb_t2)
    RadioButton rbT2;
    @Bind(R.id.rb_t3)
    RadioButton rbT3;
    @Bind(R.id.rb_t4)
    RadioButton rbT4;
    @Bind(R.id.rg_urgent)
    RadioGroup rgUrgent;
    @Bind(R.id.et_sla_day)
    EditText etSlaDay;
    @Bind(R.id.et_sla_hour)
    EditText etSlaHour;
    @Bind(R.id.et_describe)
    EditText etDescribe;
    @Bind(R.id.tv_describe)
    TextView tvDescribe;
    @Bind(R.id.et_instruction)
    EditText etInstruction;
    @Bind(R.id.tv_instruction)
    TextView tvInstruction;
    @Bind(R.id.iv_send_company)
    ImageView ivSendCompany;
    @Bind(R.id.iv_send_person)
    ImageView ivSendPerson;
    @Bind(R.id.iv_deal_company)
    ImageView ivDealCompany;
    @Bind(R.id.scroll_view)
    ScrollView scrollView;
    //endregion
    private String bindFlag = "";  // bindFlag 是否绑定  1 绑定外协  2不是外协
    private String hardwareName;
    private String hardwareId;
    private String ownerCompanyCode = "";  //发起公司code
    private String ownerEmployeeId = ""; // 发起人ID
    private String serviceCompanyCode; // 处理公司code
    private String serviceEmployeeId = ""; // 处理人ID
    private String contractId = ""; // 项目id
    private String levelId; // 故障等级ID
    private String levelName;
    private String tempCategoryId = ""; // SLA指标模板分类ID
    private String outsourceFlag;
    private String isInstead;
    private MuchObjectAdapter adapter;
    private int mPosition; // 记录操作的是哪个对象
    private List<SendSLABean.RowsBean> slaBean;  // sla等级
    private String assetId = "";
    private int rbId = -1;

    /**
     * 获取 发起工单需要的数据
     */
    public HashMap<String, String> getSendWorkInfo() {

        if (TextUtils.isEmpty(ownerCompanyCode)) {
            showCenterInfoMsg("请选择业主单位");
            return null;
        }
        if (TextUtils.isEmpty(ownerEmployeeId)) {
            showCenterInfoMsg("请选择发起人");
            return null;
        }
        if (TextUtils.isEmpty(serviceCompanyCode)) {
            showCenterInfoMsg("请选择处理企业");
            return null;
        }
        if (TextUtils.isEmpty(levelId)) {
            showCenterInfoMsg("请选择故障等级");
            return null;
        }
        String slaDay = etSlaDay.getText().toString().trim();
        String slaHour = etSlaHour.getText().toString().trim();
        if (TextUtils.isEmpty(slaDay) || TextUtils.isEmpty(slaHour)) {
            showCenterInfoMsg("请输入SLA");
            return null;
        }
        if (rbId != -1) {
            if (Integer.parseInt(slaDay) < slaBean.get(rbId).getCompleteDay()) {
                showCenterInfoMsg("处理SLA要求不能小于" + slaBean.get(rbId).getCompleteDay() + "天" + slaBean.get(rbId).getCompleteHour() + "小时");
                return null;
            } else if (Integer.parseInt(slaDay) == slaBean.get(rbId).getCompleteDay()) {
                if (Integer.parseInt(slaHour) < slaBean.get(rbId).getCompleteHour()) {
                    showCenterInfoMsg("处理SLA要求不能小于" + slaBean.get(rbId).getCompleteDay() + "天" + slaBean.get(rbId).getCompleteHour() + "小时");
                    return null;
                }
            }
        }
        if (TextUtils.isEmpty(etDescribe.getText().toString())) {
            showCenterInfoMsg("请输入简要描述");
            return null;
        }

        for (MuchAssetBean bean : adapter.getData()) {
            for (MuchAssetBean bean1 : adapter.getData()) {
                if (!bean.equals(bean1)) {
                    if (!TextUtils.isEmpty(bean.getAssetId()) && bean.getAssetId().equals(bean1.getAssetId())) {
                        if (TextUtils.isEmpty(bean.getDirectoryId()) && TextUtils.isEmpty(bean1.getDirectoryId())) {
                            showCenterInfoMsg("服务对象和服务目录重复，请重新选择");
                            return null;
                        }
                        if ((!TextUtils.isEmpty(bean.getDirectoryId()) && bean.getDirectoryId().equals(bean1.getDirectoryId()))) {
                            showCenterInfoMsg("服务对象和服务目录重复，请重新选择");
                            return null;
                        }
                    }
                }
            }
        }

        // 多对象 json
        List<HashMap<String, String>> list = new ArrayList<>();
        for (MuchAssetBean bean : adapter.getData()) {
            if (!TextUtils.isEmpty(bean.getAssetId())) {
                HashMap<String, String> objMap = new HashMap<>();
                objMap.put("assetId", bean.getAssetId());
                objMap.put("relationId", bean.getRelationId());
                objMap.put("directoryId", bean.getDirectoryId());
                list.add(objMap);
            }
        }
        String hardWareIds = "";
        if (list.size() != 0) {
            Gson gson = new Gson();
            hardWareIds = gson.toJson(list);
        }

        HashMap<String, String> map = new HashMap<>();
        map.put("ownerCode", ownerCompanyCode);
        map.put("sendUser", ownerEmployeeId);
        map.put("serviceProviderCode", serviceCompanyCode);
        map.put("processPeopleReq", serviceEmployeeId);
        map.put("contractId", contractId);
        map.put("hardWareIds", hardWareIds); //多对象
        map.put("levelId", levelId);
        map.put("days", slaDay);
        map.put("hours", slaHour);
        map.put("tempCategoryId", tempCategoryId);
        map.put("title", etDescribe.getText().toString());
        map.put("requestContent", etInstruction.getText().toString());
        map.put("outsourceFlag", outsourceFlag);
        map.put("isInstead", isInstead);
        if ("1".equals(companyType)) {
            map.put("currentUserId", ownerBean.getRows().getEmployeeId());
        } else {
            map.put("levelName", levelName);
            map.put("currentUserId", serviceLoginBean.getRows().getEmployeeId());
        }
        return map;
    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_send_work_detail;
    }

    @Override
    protected void initView(View view, Bundle savedInstanceState) {
        Intent intent = getActivity().getIntent();
        if (intent != null) {
            hardwareName = intent.getStringExtra("hardwareName");
            hardwareId = intent.getStringExtra("hardwareId");
        }

        recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
        recyclerView.setNestedScrollingEnabled(false);
        adapter = new MuchObjectAdapter(getContext());
        adapter.setOnItemClickLitener(this);
        recyclerView.setAdapter(adapter);

        MuchAssetBean item = new MuchAssetBean();
        if (!TextUtils.isEmpty(hardwareId)) {
            item.setAssetId(hardwareId);
            item.setAssetName(hardwareName);
        }
        adapter.addLastData(item); //默认存在一个
        // 滚动到顶部
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                scrollView.scrollTo(0, 0);
            }
        }, 10);

        tvTitleProject.setOnClickListener(this);
        tvUrgent.setOnClickListener(this);
        rgUrgent.setOnCheckedChangeListener(this);
        etSlaDay.addTextChangedListener(etslaDayListener);
        etDescribe.setFilters(new InputFilter[]{new InputFilter.LengthFilter(50), AppHelper.emojiFilter});
        etDescribe.addTextChangedListener(etDescribeListener);
        etInstruction.setFilters(new InputFilter[]{new InputFilter.LengthFilter(200), AppHelper.emojiFilter});
        etInstruction.addTextChangedListener(etInstructionListener);
        etInstruction.setOnTouchListener(this);

        rlSendCompany.setOnClickListener(this);
        rlSendPerson.setOnClickListener(this);
        rlDealCompany.setOnClickListener(this);
        rlDealPerson.setOnClickListener(this);
        rlProject.setOnClickListener(this);
        rlObjectAdd.setOnClickListener(this);

        if ("1".equals(companyType)) {
            tvSendCompany.setText(ownerBean.getRows().getCompany().getCompanyName());
            tvSendPerson.setText(ownerBean.getRows().getName());
            // 默认发起人和业主单位
            ownerCompanyCode = ownerBean.getRows().getOwnerCode();
            ownerEmployeeId = ownerBean.getRows().getEmployeeId();
            // 隐藏项目
            rlProject.setVisibility(View.GONE);
            // 隐藏 箭头控件
            ivSendCompany.setVisibility(View.INVISIBLE);
            ivSendPerson.setVisibility(View.INVISIBLE);
            // 取消点击事件
            rlSendCompany.setOnClickListener(null);
            rlSendPerson.setOnClickListener(null);
        } else if ("2".equals(companyType)) {
            tvDealCompany.setText(serviceLoginBean.getRows().getCompany().getCompanyName());
            // 默认处理服务商
            serviceCompanyCode = serviceLoginBean.getRows().getServiceProviderCode();
            // 展示 箭头控件
            ivDealCompany.setVisibility(View.INVISIBLE);
            // 取消点击事件
            rlDealCompany.setOnClickListener(null);
            // 从对象发起工单 筛选
            if (intent != null) {
                tvSendCompany.setText(StringUtils.value(intent.getStringExtra("companyName")));
                bindFlag = intent.getStringExtra("bindFlag");
                ownerCompanyCode = intent.getStringExtra("companyCode");
                initOut();
            }
        }


        // 获取SLA 故障等级
        getSLAData();

    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.tv_title_project:
                GuideWebActivity.openActivity(getContext(), Api.$().GUIDE_PROJECT_URI);
                break;
            case R.id.tv_urgent:
                GuideWebActivity.openActivity(getContext(), Api.$().GUIDE_SLA_URI);
                break;
            case R.id.rl_object_add:
                if (adapter.getItemCount() == 10) {
                    showCenterInfoMsg("最多允许添加10个对象");
                    return;
                }
                adapter.addLastData(new MuchAssetBean());
                break;
            case R.id.rl_send_company: //发起企业
                CompanySelectActivity.OpenActivity(this, CompanySelectActivity.FROM_SENDER, START_FOR_OWNER_CHOOSE);
                break;
            case R.id.rl_send_person:  // 发起人
                if (TextUtils.isEmpty(bindFlag) && TextUtils.isEmpty(ownerCompanyCode)) {
                    showCenterInfoMsg("请选择业主单位");
                } else {
                    PersonSelectActivity.OpenActivity(this, PersonSelectActivity.FROM_LAUNCH, bindFlag, ownerCompanyCode, START_FOR_OWNER_PERSON_CHOOSE);
                }
                break;
            case R.id.rl_deal_company: //处理企业
                CompanySelectActivity.OpenActivity(this, CompanySelectActivity.FROM_DEALER, START_FOR_SERVICE_CHOOSE);
                break;
            case R.id.rl_deal_person: // 处理人
                if (TextUtils.isEmpty(ownerCompanyCode)) {//判断业主是否为空
                    showCenterInfoMsg("请选择业主企业");
                } else if (TextUtils.isEmpty(serviceCompanyCode)) {
                    showCenterInfoMsg("请选择处理企业");
                } else if ("1".equals(bindFlag) && "1".equals(companyType)) { // 判断是否是外协单
                    showCenterInfoMsg("外协工单不允许选择处理人");
                } else {//判断是权限
                    if ("1".equals(companyType)) {//判断公司类型
                        if (ownerBean.getRows() != null && ownerBean.getRows().getEmployee()
                                != null && ownerBean.getRows().getEmployee().getRoleId()
                                != null && "12".equals(ownerBean.getRows().getEmployee().getRoleId()) || "10".equals(ownerBean.getRows().getEmployee().getRoleId())) {
                            PersonSelectActivity.OpenActivity(this, PersonSelectActivity.FROM_DEAL, START_FOR_SERVICE_PERSON_CHOOSE);
                        } else {
                            showCenterInfoMsg("只有管理员、服务台可以选到处理人");
                        }
                    } else {
                        if (serviceLoginBean.getRows() != null && serviceLoginBean.getRows().getEmployee()
                                != null && serviceLoginBean.getRows().getEmployee().getRoleId()
                                != null && "12".equals(serviceLoginBean.getRows().getEmployee().getRoleId()) || "10".equals(serviceLoginBean.getRows().getEmployee().getRoleId())) {
                            PersonSelectActivity.OpenActivity(this, PersonSelectActivity.FROM_DEAL, START_FOR_SERVICE_PERSON_CHOOSE);
                        } else {
                            showCenterInfoMsg("只有管理员、服务台可以选到处理人");
                        }
                    }
                }
                break;
            case R.id.rl_project:  // 项目
                assetId = "";
                List<MuchAssetBean> list = adapter.getData();
                for (MuchAssetBean bean : list) {
                    if (!TextUtils.isEmpty(bean.getAssetId())) {
                        assetId += bean.getAssetId() + ",";
                    }
                }
                if (!TextUtils.isEmpty(assetId)) {
                    assetId = assetId.substring(0, assetId.length() - 1);
                }
                ProjectSelectActivity.OpenActivity(this, ownerCompanyCode, contractId, assetId,
                        START_FOR_SERVICE_PROJECT_CHOOSE);
                break;
            default:
                break;
        }
    }

    @Override
    public void onItemClick(View view, int position) {
        mPosition = position;
        MuchAssetBean bean = adapter.getItem(position);
        switch (view.getId()) {
            case R.id.rl_object_delete: // 删除对象
                if (adapter.getItemCount() == 1) {
                    showCenterInfoMsg("至少需要一个对象");
                    return;
                }
                adapter.removeData(position);
                break;
            case R.id.rl_object: // 对象
                ObjectSelectActivity.OpenActivity(this, ownerCompanyCode,
                        contractId, START_FOR_SERVICE_OBJECT_CHOOSE);
                break;
            case R.id.rl_service: // 服务目录
                if (TextUtils.isEmpty(bean.getAssetId())) {
                    showCenterInfoMsg("请选择服务对象");
                } else {
                    ServiceSelectActivity.OpenActivity(this, ServiceSelectActivity.FROM_WORK_NORMAL, contractId, bean.getAssetId(), ownerCompanyCode,
                            bean.getDirectoryId(), START_FOR_SERVICE_DIR_CHOOSE);
                }
                break;
            case R.id.tv_title_service:
                GuideWebActivity.openActivity(getContext(), Api.$().GUIDE_SERVICE_URI);
                break;
            default:
                break;
        }

    }

    @Override
    public void onCheckedChanged(RadioGroup radioGroup, @IdRes int id) {
        switch (id) {
            case R.id.rb_t1:
                rbId = 0;
                break;
            case R.id.rb_t2:
                rbId = 1;
                break;
            case R.id.rb_t3:
                rbId = 2;
                break;
            case R.id.rb_t4:
                rbId = 3;
                break;
            default:
                break;
        }
        if (rbId != -1) {
            if (slaBean != null && slaBean.size() != 0) {
                SendSLABean.RowsBean bean = slaBean.get(rbId);
                levelId = bean.getTempId();
                levelName = bean.getLevelName();
                tempCategoryId = bean.getTempCategoryId() + "";
                etSlaDay.setText(bean.getCompleteDay() + "");
                etSlaHour.setText(bean.getCompleteHour() + "");
            }
        }

    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == Activity.RESULT_OK) {
            Bundle bundle = null;
            if (data != null) {
                bundle = data.getExtras();
            }
            if (requestCode == START_FOR_OWNER_CHOOSE) { // 选择业主
                tvSendCompany.setText(bundle.getString("companyName"));
                bindFlag = bundle.getString("bindFlag");
                ownerCompanyCode = bundle.getString("companyCode");
                initOut();
                tvSendPerson.setText("");//发起人
                ownerEmployeeId = "";
                // 项目
                tvProject.setText("");
                contractId = "";
                adapter.clean();
                slaClean();
            } else if (requestCode == START_FOR_OWNER_PERSON_CHOOSE) { // 选择发起人
                if (!TextUtils.isEmpty(bundle.getString("name"))) {
                    tvSendPerson.setText(bundle.getString("name"));
                    ownerEmployeeId = bundle.getString("employeeId");
                }
            } else if (requestCode == START_FOR_SERVICE_CHOOSE) {
                if (!TextUtils.isEmpty(bundle.getString("companyName"))) {
                    tvDealCompany.setText(bundle.getString("companyName"));
                    serviceCompanyCode = bundle.getString("companyCode");
                    bindFlag = bundle.getString("bindFlag");
                    if ("1".equals(bindFlag)) {  //外协
                        outsourceFlag = "1";
                        isInstead = "1";
                        rlObjectAdd.setVisibility(View.GONE);
                        adapter.clean();
                    } else { // 内
                        outsourceFlag = "0";
                        isInstead = "0";
                        rlObjectAdd.setVisibility(View.VISIBLE);
                    }

                    tvDealPerson.setText("");
                    serviceEmployeeId = "";
                }
            } else if (requestCode == START_FOR_SERVICE_PERSON_CHOOSE) {
                if (!TextUtils.isEmpty(bundle.getString("name"))) {
                    tvDealPerson.setText(bundle.getString("name"));
                    serviceEmployeeId = bundle.getString("employeeId");
                }
            } else if (requestCode == START_FOR_SERVICE_PROJECT_CHOOSE) { //项目
                tvProject.setText(bundle.getString("contractName"));
                contractId = bundle.getString("contractId");

                if (!TextUtils.isEmpty(bundle.getString("contractId"))) {
                    //联动 展示请求企业
                    tvSendCompany.setText(bundle.getString("serverName"));
                    ownerCompanyCode = bundle.getString("ownerCode");
                    bindFlag = bundle.getString("bindFlag");
                    initOut();
                    if (!TextUtils.isEmpty(assetId)) {
                        // 对象联动
                        getHandwareIds();
                    }
                }
                // 获取sla
                slaClean();
                getSLAData();

            } else if (requestCode == START_FOR_SERVICE_OBJECT_CHOOSE) {  //对象
                if (!TextUtils.isEmpty(bundle.getString("hardwareId"))) {
                    // 是否从扫码过来
                    boolean isScreen = bundle.getBoolean("isScreen");
                    if (isScreen) {
                        // 判断发起企业是否一致
                        String ownerCode = bundle.getString("ownerCode");
                        if (TextUtils.isEmpty(ownerCompanyCode) || (!TextUtils.isEmpty(ownerCode) && ownerCode.equals(ownerCompanyCode))) {
                            if ("2".equals(companyType)) {
                                tvSendCompany.setText(bundle.getString("serverName"));
                                bindFlag = bundle.getString("bindFlag");
                                ownerCompanyCode = ownerCode;
                                initOut();
                                // 清空项目
                                contractId = "";
                                tvProject.setText("");
                            }
                        } else {
                            showCenterInfoMsg("该对象不属于当前请求企业");
                            return;
                        }
                    }

                    MuchAssetBean bean = adapter.getItem(mPosition);
                    bean.setAssetName(bundle.getString("name"));
                    bean.setAssetId(bundle.getString("hardwareId"));
                    bean.setDirectoryName("");
                    bean.setDirectoryId("");
                    bean.setRelationId("");
                    bean.setSolutionState("");

                    adapter.notifyItemChanged(mPosition);
                }

            } else if (requestCode == START_FOR_SERVICE_DIR_CHOOSE) { //fuwu
                MuchAssetBean bean = adapter.getItem(mPosition);
                if (!TextUtils.isEmpty(bundle.getString("directoryName"))) {
                    bean.setDirectoryName(bundle.getString("directoryName"));
                    bean.setDirectoryId(bundle.getString("directoryId"));
                    bean.setRelationId(bundle.getString("relationIds"));
                }
                if (!TextUtils.isEmpty(bundle.getString("completeTimes")) && !TextUtils.isEmpty(bundle.getString("type"))) {
                    if ("1".equals(bundle.getString("type"))) {
                        bean.setSolutionState(bundle.getString("completeTimes") + "次");
                    } else if ("2".equals(bundle.getString("type"))) {
                        bean.setSolutionState(bundle.getString("completeTimes") + "人/天");
                    } else {
                        bean.setSolutionState("0次");
                    }
                } else {
                    bean.setSolutionState("0次");
                }
                adapter.notifyItemChanged(mPosition);
            }


        }

    }

    private void initOut() {
        if ("1".equals(bindFlag)) {  //外协
            outsourceFlag = "1";
            isInstead = "1";
        } else { // 内
            outsourceFlag = "0";
            isInstead = "0";
        }
    }

    // 获取服务对象联动
    private void getHandwareIds() {
        HashMap<String, String> map = new HashMap<>();
        map.put("contractId", contractId);
        map.put("assetId", assetId);
        map.put("currentUserId", serviceLoginBean.getRows().getEmployeeId());
        ServiceRequestManager.getManager().getSendWorkHandware(getContext(), JsonMapUtil.mapToJson(map), this);
    }

    // 获取sla 数据
    private void getSLAData() {
        HashMap<String, String> map = new HashMap<>();
        if ("1".equals(companyType)) {
            map.put("ownerCode", ownerBean.getRows().getOwnerCode());
        } else {
            map.put("contractId", contractId);
            map.put("serviceProviderCode", serviceLoginBean.getRows().getServiceProviderCode());
        }
        ServiceRequestManager.getManager().getSendWorkSLA(getContext(), JsonMapUtil.mapToJson(map), this);
    }

    //清空 紧急、sla条件
    private void slaClean() {
        if ("2".equals(companyType)) {
            // 紧急程度
            rgUrgent.clearCheck();
            levelId = "";
            // SLA
            etSlaDay.setText("");
            etSlaHour.setText("");
            tempCategoryId = "";
        }
    }

    @Override
    public void onSuccess(int requestId, String result) {
        switch (requestId) {
            case 1:
                SendSLABean sendSLABean = new Gson().fromJson(result, SendSLABean.class);
                if (sendSLABean.getMessage().getCode() == 200) {
                    slaBean = sendSLABean.getRows();
                }
                break;
            case 2: // 服务对象
                AssetBean bean = new Gson().fromJson(result, AssetBean.class);
                List<MuchAssetBean> list = new ArrayList<>();
                if (bean.getRows() != null && bean.getRows().size() != 0) {
                    for (AssetBean.RowsBean rowsBean : bean.getRows()) {
                        for (MuchAssetBean objectBean : adapter.getData()) {
                            if (rowsBean.getAssetId().equals(objectBean.getAssetId())) {
                                list.add(objectBean);
                                break;
                            }
                        }
                    }
                }
                adapter.setData(list);
                break;
            default:
                break;
        }
    }

    @Override
    public void onException(int requestId, String code) {
        showTopWrongMsg(getContext().getResources().getString(R.string.net_work_error));
    }

    @Override
    public boolean onTouch(View view, MotionEvent motionEvent) {
        //触摸的是EditText并且当前EditText可以滚动则将事件交给EditText处理；否则将事件交由其父类处理
        if ((view.getId() == R.id.et_instruction && canVerticalScroll(etInstruction))) {
            view.getParent().requestDisallowInterceptTouchEvent(true);
            if (motionEvent.getAction() == MotionEvent.ACTION_UP) {
                view.getParent().requestDisallowInterceptTouchEvent(false);
            }
        }
        return false;
    }

    /**
     * EditText竖直方向是否可以滚动
     *
     * @param editText 需要判断的EditText
     * @return true：可以滚动   false：不可以滚动
     */
    private boolean canVerticalScroll(EditText editText) {
        //滚动的距离
        int scrollY = editText.getScrollY();
        //控件内容的总高度
        int scrollRange = editText.getLayout().getHeight();
        //控件实际显示的高度
        int scrollExtent = editText.getHeight() - editText.getCompoundPaddingTop() - editText.getCompoundPaddingBottom();
        //控件内容总高度与实际显示高度的差值
        int scrollDifference = scrollRange - scrollExtent;

        if (scrollDifference == 0) {
            return false;
        }

        return (scrollY > 0) || (scrollY < scrollDifference - 1);
    }

    private TextWatcher etslaDayListener = new TextWatcher() {
        @Override
        public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

        }

        @Override
        public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
            if (!TextUtils.isEmpty(charSequence.toString())) {
                int day = Integer.parseInt(charSequence.toString());
                if (day > 24) {
                    etSlaDay.setText("24");
                }
            }
        }

        @Override
        public void afterTextChanged(Editable editable) {

        }
    };
    private TextWatcher etDescribeListener = new TextWatcher() {
        @Override
        public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

        }

        @Override
        public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
            tvDescribe.setText(charSequence.toString().length() + "");
        }

        @Override
        public void afterTextChanged(Editable editable) {

        }
    };
    private TextWatcher etInstructionListener = new TextWatcher() {
        @Override
        public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

        }

        @Override
        public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
            tvInstruction.setText(charSequence.toString().length() + "");
        }

        @Override
        public void afterTextChanged(Editable editable) {

        }
    };
}
